package com.leyou.auth.controller;

import com.leyou.auth.service.AuthServcie;
import com.leyou.common.auth.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: xiang
 * @Date: 2019/8/29 20:12
 * @Description:
 */
@RestController
public class AuthController {
    @Autowired
    private AuthServcie authServcie;
/**
 *
 * 登录授权
 *    * @param username 用户名
 * @param password 密码
 * @return 无
 */
@PostMapping("login")
    public ResponseEntity<Void> login( @RequestParam("username") String username,
                                       @RequestParam("password") String password,
                                       HttpServletResponse response){
authServcie.login(username,password,response);
    return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 验证用户信息
     * @param request
     * @param response
     * @return
     */
    @GetMapping("verify")
    public ResponseEntity<UserInfo> verifyUser(HttpServletRequest request ,HttpServletResponse response){

    return  ResponseEntity.ok(authServcie.verifyUser(request,response));
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("logout")
    public ResponseEntity<Void> logout(HttpServletRequest request,HttpServletResponse response){
        authServcie.logout(request, response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();


    }

}